/** ShogiBoard
 * 
 * @author Charles Ritchea
 */
package shogi;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

public class ShogiBoard extends JFrame implements MouseListener, MouseMotionListener{
	/** control provides methods for gameplay like move legality */
	private ShogiController control;
	/** dragPane is used with mouse listeners for drag and drop functionality */
	private JLayeredPane dragPane;
	/** gameBoard JPanel holds the 81 ShogiSquares */
	private JPanel gameBoard;
	/**  leftCapturedPane holds left side captured pieces */
	private JPanel leftCapturedPane;
	/**  rightCapturedPane holds right side captured pieces */
	private JPanel rightCapturedPane;
	/** aCapturedJPanel */
	private JPanel aCapturedJPanel;
	/** aCapturedJButton */
	private JButton aCapturedJButton;
	/** aShogiPiece JLabel represents any active piece on the board */
	private ShogiPiece aShogiPiece;
	private ShogiPiece dropPiece;
	/** aShogiSquare JPanel represents any active square on the board */
	private ShogiSquare aShogiSquare;
	/** xOffset is the horizontal offset between the location of a square and the cursor */
	private int xOffset;
	/** yOffset is the vertical offset between the location of a square and the cursor */
	private int yOffset;
	/** originalLocation is the Point where the mouse is pressed */
	private Point originalLocation;
	/** destinationLocation is the Point where the mouse is released */
	private Point destinationLocation;
	/** isLeftsTurn holds the value to determine which side may move a piece */
        boolean isLeftsTurn;
	/** originalSquare is the square a piece was in before being moved */
        private ShogiSquare originalSquare;
	/** iconSetDirectory is a string for the directory of a particular icon theme*/
	private static String iconSetDirectory;
	/** openingBoard is a string containing the opening setup */
	private char[] openingBoard;
	/**the default color of a ShogiSquare */
	private Color defaultColor;
	/**the color of a ShogiSquare that a ShogiPiece may move to*/
	private Color legalColor;
	/**the color of the left players occupied ShogiSquares */
	private Color leftColor;
	/**the color of the right players occupied ShogiSquares */
	private Color rightColor;
	/**the save game*/
	private char[] saveGame;
	/** ShogiBoard() initializes an empty gameBoard then 
	 *  calls resetBoard to fill the gameBoard with 81
	 *  squares and the opening pieces
	 * 
	 * @param openingBoard
	 * @param iconSetDirectory  
	 */
	public ShogiBoard(String openingBoard, String iconSetDirectory)
	{
		super("Gaijin Shogi");
		this.openingBoard = openingBoard.toCharArray();
		saveGame = this.openingBoard;
		this.iconSetDirectory = iconSetDirectory;
		getContentPane().setLayout(new BorderLayout());
		initializeLeftCapturedPane();
		initializeRightCapturedPane();
		Dimension paneSize = new Dimension(650, 550);
		dragPane = new JLayeredPane();
		dragPane.setPreferredSize( paneSize );
		dragPane.addMouseListener( this );
		dragPane.addMouseMotionListener( this );
		dragPane.setLayout(new BoxLayout(dragPane, BoxLayout.X_AXIS));
		Dimension boardSize = new Dimension(650, 550);
		gameBoard = new JPanel();
		gameBoard.setLayout( new GridLayout(9, 9) );
		gameBoard.setPreferredSize(boardSize );
		gameBoard.setBounds(0, 0, boardSize.width, boardSize.height);
		dragPane.add(gameBoard, JLayeredPane.DEFAULT_LAYER);
		getContentPane().add(dragPane, BorderLayout.CENTER);
		control = new ShogiController();
		legalColor = Color.BLUE;
		initializeBoard();
		setBoard();
	}
	private void initializeLeftCapturedPane(){
		Dimension leftCapturedSize = new Dimension(85, 550);
		leftCapturedPane = new JPanel();
		leftCapturedPane.setLayout( new GridLayout(7,1));
		leftCapturedPane.setPreferredSize(leftCapturedSize);
		for(int i = 0; i < 7; i++){
			aCapturedJPanel = new JPanel();
			aCapturedJPanel.setBackground(Color.white);
			aCapturedJPanel.setBorder(new LineBorder(Color.BLACK));
			aCapturedJButton = new JButton(Character.toString(openingBoard[i+81]));
			aCapturedJButton.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
			char type;
			switch(i){
				case 0:
					type = 'p';
					break;
				case 1:
					type = 'l';
					break;
				case 2:
					type = 'n';
					break;
				case 3:
					type = 's';
					break;
				case 4:
					type = 'g';
					break;
				case 5:
					type = 'b';
					break;
				case 6:
					type = 'r';
					break;
				default:
					type = 'k';
					break;
			}
			aCapturedJButton.setIcon(new ImageIcon(getClass().getResource(iconSetDirectory+type+"Left"+".png")));
			aCapturedJButton.setName(""+type);
			aCapturedJButton.setContentAreaFilled(false);
			aCapturedJButton.setBorder(null);
			aCapturedJButton.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					if(!isLeftsTurn) return;
					JButton source = (JButton)e.getSource();
					if(Integer.parseInt(source.getText()) > 0){
						dropPiece = new ShogiPiece(0, source.getName().toCharArray()[0], "Left");
					}
				}

			});
			aCapturedJPanel.setPreferredSize(aCapturedJButton.getSize());
			aCapturedJPanel.add(aCapturedJButton);
			leftCapturedPane.add(aCapturedJPanel);
		}
		getContentPane().add(leftCapturedPane, BorderLayout.WEST);

	}
	private void initializeRightCapturedPane(){
		Dimension rightCapturedSize = new Dimension(85, 550);
		rightCapturedPane = new JPanel();
		rightCapturedPane.setLayout( new GridLayout(7,1));
		rightCapturedPane.setPreferredSize(rightCapturedSize);
		for(int i = 0; i < 7; i++){
			aCapturedJPanel = new JPanel();
			aCapturedJPanel.setBackground(Color.white);
			aCapturedJPanel.setBorder(new LineBorder(Color.BLACK));
			aCapturedJButton = new JButton(Character.toString(openingBoard[i+81+7]));
			char type;
			switch(i){
				case 0:
					type = 'p';
					break;
				case 1:
					type = 'l';
					break;
				case 2:
					type = 'n';
					break;
				case 3:
					type = 's';
					break;
				case 4:
					type = 'g';
					break;
				case 5:
					type = 'b';
					break;
				case 6:
					type = 'r';
					break;
				default:
					type = 'k';
					break;
			}
			aCapturedJButton.setIcon(new ImageIcon(getClass().getResource(iconSetDirectory+type+"Right"+".png")));
			aCapturedJButton.setName(""+type);
			aCapturedJButton.setContentAreaFilled(false);
			aCapturedJButton.setBorder(null);
			aCapturedJButton.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					if(isLeftsTurn) return;
					JButton source = (JButton)e.getSource();
					if(Integer.parseInt(source.getText()) > 0){
						dropPiece = new ShogiPiece(0, source.getName().toCharArray()[0], "Right");
					}
				}

			});
			aCapturedJPanel.setPreferredSize(aCapturedJButton.getSize());
			aCapturedJPanel.add(aCapturedJButton);
			rightCapturedPane.add(aCapturedJPanel);
		}
		getContentPane().add(rightCapturedPane, BorderLayout.EAST);

	}
	/** initializes and empty board
	 * 
	 */
	private void initializeBoard(){
		for (int i = 0; i < 81; i++)
		{
			int row = (i / 9);
			int column = (i % 9);
			control.setLeftPlacement(i,0);
			control.setRightPlacement(i,0);
			aShogiSquare = new ShogiSquare(Color.WHITE, i, new BorderLayout() );
			aShogiSquare.setBorder(new LineBorder(Color.BLACK));
			aShogiSquare.setIndex(i);
			gameBoard.add( aShogiSquare );
			if(openingBoard[openingBoard.length - 1] == '1'){
                        	isLeftsTurn = true;
			} else {
				isLeftsTurn = false;
			}
			defaultColor = Color.orange;
			aShogiSquare.setBackground(defaultColor);
		}
	}
	/**
	 * 
	 * @param openingBoard
	 */
	public void loadBoard(String openingBoard){
		this.openingBoard = openingBoard.toCharArray();
		for(int i = 0; i < 7; i++){
			((JButton)((JPanel)leftCapturedPane.getComponent(i)).getComponent(0)).setText(""+this.openingBoard[i+81]);
		}
		for(int i = 0; i < 7; i++){
			((JButton)((JPanel)rightCapturedPane.getComponent(i)).getComponent(0)).setText(""+this.openingBoard[i+81+7]);
		}
			if(this.openingBoard[this.openingBoard.length - 1] == '1'){
                        	isLeftsTurn = true;
			} else {
				isLeftsTurn = false;
			}
		setBoard();
	}
	/** Places pieces in their saved position
	 * 
	 */
	private void setBoard(){
		if(isLeftsTurn){
			leftColor = Color.GREEN;
			rightColor = Color.RED;
		}else{
			leftColor = Color.RED;
			rightColor = Color.GREEN;
		}
		for(int i = 0; i < 81; i++){
			control.setLeftPlacement(i, 0);
			control.setRightPlacement(i, 0);
			if(Character.isLowerCase(openingBoard[i])){
				aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
				aShogiSquare.removeAll();
				aShogiSquare.setBackground(defaultColor);
				switch(openingBoard[i]){
					case'l':aShogiPiece = new ShogiPiece(i, 'l',"Left");
						control.setLeftPlacement(i, 1);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(leftColor);
						break;
					case'n':aShogiPiece = new ShogiPiece(i, 'n',"Left");
						control.setLeftPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(leftColor);
						break;
					case's':aShogiPiece = new ShogiPiece(i, 's',"Left");
						control.setLeftPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(leftColor);
						break;
					case'g':aShogiPiece = new ShogiPiece(i, 'g',"Left");
						control.setLeftPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(leftColor);
						break;
					case'k':aShogiPiece = new ShogiPiece(i, 'k',"Left");
						control.setLeftPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(leftColor);
						break;
					case'b':aShogiPiece = new ShogiPiece(i, 'b',"Left");
						control.setLeftPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(leftColor);
						break;
					case'r':aShogiPiece = new ShogiPiece(i, 'r',"Left");
						control.setLeftPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(leftColor);
						break;
					case'p':aShogiPiece = new ShogiPiece(i, 'p',"Left");
						control.setLeftPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(leftColor);
						break;
					default:aShogiSquare.setBackground(defaultColor);
						break;
				}
			} else if(Character.isUpperCase(openingBoard[i])){
				switch(openingBoard[i]){
					case'L':aShogiPiece = new ShogiPiece(i, 'l',"Right");
						control.setRightPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(rightColor);
						break;
					case'N':aShogiPiece = new ShogiPiece(i, 'n',"Right");
						control.setRightPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(rightColor);
						break;
					case'S':aShogiPiece = new ShogiPiece(i, 's',"Right");
						control.setRightPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(rightColor);
						break;
					case'G':aShogiPiece = new ShogiPiece(i, 'g',"Right");
						control.setRightPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(rightColor);
						break;
					case'K':aShogiPiece = new ShogiPiece(i, 'k',"Right");
						control.setRightPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(rightColor);
						break;
					case'B':aShogiPiece = new ShogiPiece(i, 'b',"Right");
						control.setRightPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(rightColor);
						break;
					case'R':aShogiPiece = new ShogiPiece(i, 'r',"Right");
						control.setRightPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(rightColor);
						break;
					case'P':aShogiPiece = new ShogiPiece(i, 'p',"Right");
						control.setRightPlacement(i, 1);
						aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
						aShogiSquare.add(aShogiPiece);
						aShogiSquare.setBackground(rightColor);
						break;
					default:aShogiSquare.setBackground(defaultColor);
						break;
				}
			}
		}
	}
        /** nextTurn enables the pieces that were disabled
         *  and disables the pieces that were enabled
         */
        private void nextTurn(){
            if(isLeftsTurn){
                isLeftsTurn = false;
		leftColor = Color.RED;
		rightColor = Color.GREEN;
		saveGame[saveGame.length - 1] = '0';
            }
            else{
                isLeftsTurn = true;
		leftColor = Color.GREEN;
		rightColor = Color.RED;
		saveGame[saveGame.length - 1] = '1';
            }
	    updateColors();
	    updateSaveGameCapture();
	    System.out.println(String.valueOf(saveGame));
        }
	private void updateSaveGameCapture(){
			Component[] leftSquares = getLeftCapturedPane().getComponents();
			Component[] rightSquares = getRightCapturedPane().getComponents();
		for(int i = 0; i < 7; i++){
			JPanel square = (JPanel)leftSquares[i];
			JButton button = (JButton)square.getComponent(0);
			saveGame[81+i] = button.getText().charAt(0);
			square = (JPanel)rightSquares[i];
			button = (JButton)square.getComponent(0);
			saveGame[88+i] = button.getText().charAt(0);
		}
	}
	private void updateColors(){
            for(int i = 0; i < 81; i++){
                aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
                if(control.getLeftPlacement(i) >= 1){
			aShogiSquare.setBackground(leftColor);
		}
                else if(control.getRightPlacement(i) >=1 ){
			aShogiSquare.setBackground(rightColor);
		}
		else{
			aShogiSquare.setBackground(defaultColor);
		}
            }
	}
	private void highlightLegalMoves(int originalIndex){
		control.setStopBishop(false);
		for(int i = 0; i < 81; i++){
			aShogiSquare = (ShogiSquare)gameBoard.getComponent(i);
			ShogiPiece piece = null;
			if(aShogiSquare.getComponentCount() > 0){
				piece = (ShogiPiece)aShogiSquare.getComponent(0);
				if(control.isLegal(originalIndex, i, aShogiPiece) && !piece.getOrientation().equals(aShogiPiece.getOrientation())){
					aShogiSquare.setBackground(legalColor);
				}
			}else{
				if(control.isLegal(originalIndex, i, aShogiPiece)){
					aShogiSquare.setBackground(legalColor);
				}
			}
		}
	}
	/*
	**  Add the selected shogiPiece to the dragging layer so it can be moved
	*/
		@Override
	public void mousePressed(MouseEvent e)
	{
		aShogiPiece  = null;
		Component c =  gameBoard.findComponentAt(e.getX(), e.getY());

		if (c instanceof ShogiSquare) return;
		destinationLocation = c.getParent().getLocation();
		originalLocation = new Point(e.getX(),e.getY());
		xOffset = destinationLocation.x - e.getX();
		yOffset = destinationLocation.y - e.getY();
		aShogiPiece = (ShogiPiece)c;
		if(isLeftsTurn && aShogiPiece.getOrientation().equals("Right")){
			aShogiPiece = null;
			return;
		}else if(!isLeftsTurn && aShogiPiece.getOrientation().equals("Left")){
			aShogiPiece = null;
			return;
		}
                originalSquare = (ShogiSquare)aShogiPiece.getParent();
		aShogiPiece.setLocation(e.getX() + xOffset, e.getY() + yOffset);
		aShogiPiece.setSize(aShogiPiece.getWidth(), aShogiPiece.getHeight());
		highlightLegalMoves(aShogiPiece.getIndex());
		dragPane.add(aShogiPiece, JLayeredPane.DRAG_LAYER);
	}

	/*
	**  Move the shogiPiece around
	*/
		@Override
	public void mouseDragged(MouseEvent me)
	{
		if (aShogiPiece == null) return;
		JLayeredPane source = (JLayeredPane)me.getSource();
		int x = me.getX() + xOffset;
		int y = me.getY() + yOffset;
		aShogiPiece.setLocation(me.getX() + xOffset, me.getY() + yOffset);
	}

	/*
	**  Drop the shogiPiece back onto the shogi board
	*/
		@Override
	public void mouseReleased(MouseEvent e) throws NullPointerException
	{
		updateColors();
		try{
			if (aShogiPiece == null) return;
			aShogiPiece.setVisible(false);
			Component c =  gameBoard.findComponentAt(e.getX(), e.getY());
			int originalIndex = aShogiPiece.getIndex();
                        int originalRow = (originalIndex % 9);
			int originalColumn = (originalIndex / 9);
                        int destinationIndex = originalIndex;
                        if(c instanceof ShogiSquare){
                            ShogiSquare square = (ShogiSquare)c;
                            destinationIndex = square.getIndex();
                        } else if (c instanceof ShogiPiece){
                            ShogiPiece capturedPiece = (ShogiPiece)c;
                            destinationIndex = capturedPiece.getIndex();
                        }
                        if (!control.isLegal(originalIndex, destinationIndex, aShogiPiece)){
                            originalSquare.add(aShogiPiece);
			    aShogiPiece.setVisible(true);
                            return;
                        }
			if (c instanceof ShogiPiece)
			{
				ShogiPiece capturedPiece = (ShogiPiece)c;
				ShogiSquare destinationSquare = (ShogiSquare)c.getParent();
                                if (capturedPiece.getOrientation().equalsIgnoreCase(aShogiPiece.getOrientation())){
				    c = gameBoard.findComponentAt(originalLocation);
                                    destinationSquare = (ShogiSquare)c;
                                    destinationSquare.add(aShogiPiece);
				    aShogiPiece.setVisible(true);
                                    return;
                                }
				aShogiPiece.setIndex(destinationSquare.getIndex());
				saveGame[originalIndex] = 'x';
                                if(aShogiPiece.getOrientation().equalsIgnoreCase("Left")){
				    control.setLeftPlacement(originalIndex, 0);
				    control.setLeftPlacement(aShogiPiece.getIndex(), 1);
				    saveGame[aShogiPiece.getIndex()] = aShogiPiece.getType();
				    control.setRightPlacement(aShogiPiece.getIndex(), 0);
				    int i;
				    switch(capturedPiece.getType()){
					    case 'p': i = 0; break;
					    case 'l': i = 1; break;
					    case 'n': i = 2; break;
					    case 's': i = 3; break;
					    case 'g': i = 4; break;
					    case 'b': i = 5; break;
					    case 'r': i = 6; break;
					    default: i = -1;
					    	System.out.println("Left Wins!");
					    	loadBoard("lxpxxxPxLnbpxxxPRNsxpxxxPxSgxpxxxPxGkxpxxxPxKgxpxxxPxGsxpxxxPxSnrpxxxPBNlxpxxxPxL000000000000001");
						return;
				    }
				    JButton button = (JButton) ((JPanel)leftCapturedPane.getComponent(i)).getComponent(0);
				    button.setText(""+(Integer.parseInt(button.getText()) + 1));
				    
                                }
                                else{
				    control.setRightPlacement(originalIndex, 0);
				    control.setRightPlacement(aShogiPiece.getIndex(), 1);
				    saveGame[aShogiPiece.getIndex()] = Character.toUpperCase(aShogiPiece.getType());
				    control.setLeftPlacement(aShogiPiece.getIndex(), 0);
				    int i;
				    switch(capturedPiece.getType()){
					    case 'p': i = 0; break;
					    case 'l': i = 1; break;
					    case 'n': i = 2; break;
					    case 's': i = 3; break;
					    case 'g': i = 4; break;
					    case 'b': i = 5; break;
					    case 'r': i = 6; break;
					    default: i = -1;
					    	System.out.println("Right Wins!");
						loadBoard("lxpxxxPxLnbpxxxPRNsxpxxxPxSgxpxxxPxGkxpxxxPxKgxpxxxPxGsxpxxxPxSnrpxxxPBNlxpxxxPxL000000000000001");
						return;
				    }
				    JButton button = (JButton) ((JPanel)rightCapturedPane.getComponent(i)).getComponent(0);
				    button.setText(""+(Integer.parseInt(button.getText()) + 1));
                                }
				destinationSquare.removeAll();
				destinationSquare.add( aShogiPiece );
				aShogiPiece.setVisible(true);
				System.out.println(aShogiPiece.getOrientation()+" "+aShogiPiece.getLongType()+" "+originalRow+","+originalColumn+" to "+capturedPiece.getOrientation().toLowerCase()+" "+capturedPiece.getLongType()+" "+aShogiPiece.getRow()+","+aShogiPiece.getColumn()+".");
                                
			}
			else
			{
				ShogiSquare parent = (ShogiSquare)c;
				aShogiPiece.setIndex(parent.getIndex());
                                if (aShogiPiece.getIndex() == originalIndex){
                                    parent.add(new ShogiPiece(aShogiPiece.getIndex(), aShogiPiece.getType(),aShogiPiece.getOrientation()));
                                    return;
                                }
				parent.removeAll();
				parent.add( aShogiPiece );
				saveGame[originalIndex] = 'x';
                                if(aShogiPiece.getOrientation().equalsIgnoreCase("Left")){
				    control.setLeftPlacement(originalIndex, 0);
				    control.setLeftPlacement(aShogiPiece.getIndex(), 1);
				    saveGame[aShogiPiece.getIndex()] = aShogiPiece.getType();
                                }
                                else{
				    control.setRightPlacement(originalIndex, 0);
				    control.setRightPlacement(aShogiPiece.getIndex(), 1);
				    saveGame[aShogiPiece.getIndex()] = Character.toUpperCase(aShogiPiece.getType());
                                }
				System.out.println(aShogiPiece.getOrientation()+" "+aShogiPiece.getLongType()+" "+originalRow+","+originalColumn+" to "+aShogiPiece.getRow()+","+aShogiPiece.getColumn()+".");
                                
			}
                        nextTurn();
			aShogiPiece.setVisible(true);

		//If shogiPiece dropped off board return to original square
		} catch(NullPointerException npe){
			Component c = gameBoard.findComponentAt(originalLocation);
			Container parent = (Container)c;
			parent.add(new ShogiPiece(aShogiPiece.getIndex(), aShogiPiece.getType(),aShogiPiece.getOrientation()));
		}
	}
		@Override
	public void mouseClicked(MouseEvent e) throws NullPointerException {
		try{
			if(dropPiece == null) return;
			Component c =  gameBoard.findComponentAt(e.getX(), e.getY());
			if(c instanceof ShogiPiece){
				return;
			}
			else if(c instanceof ShogiSquare){
				aShogiSquare = (ShogiSquare)c;
				if(aShogiSquare.getComponentCount() == 0){
					aShogiPiece = dropPiece;
					dropPiece = null;
					aShogiPiece.setIndex(aShogiSquare.getIndex());
					if(aShogiPiece.getOrientation().equalsIgnoreCase("Left")){
						JPanel panel = (JPanel)leftCapturedPane.getComponent(0);
						JButton button = (JButton)panel.getComponent(0);
						int buttonQuantity = Integer.parseInt(button.getText());
						button.setText(Integer.toString(--buttonQuantity));
						control.setLeftPlacement(aShogiPiece.getIndex(), 1);
						saveGame[aShogiPiece.getIndex()] = aShogiPiece.getType();
					}
					else{
						JPanel panel = (JPanel)rightCapturedPane.getComponent(0);
						JButton button = (JButton)panel.getComponent(0);
						int buttonQuantity = Integer.parseInt(button.getText());
						button.setText(Integer.toString(--buttonQuantity));
					    	control.setRightPlacement(aShogiPiece.getIndex(), 1);
						saveGame[aShogiPiece.getIndex()] = Character.toUpperCase(aShogiPiece.getType());
					}
					aShogiSquare.removeAll();
					aShogiSquare.add(aShogiPiece);
					aShogiSquare.setVisible(true);
					nextTurn();
					aShogiPiece.setVisible(true);
				}
				else return;
			}
			else
				return;
		}
		catch(NullPointerException npe){
			return;
		}
	}
		@Override
	public void mouseMoved(MouseEvent e) {}
		@Override
	public void mouseEntered(MouseEvent e) {
		}
		@Override
	public void mouseExited(MouseEvent e) {}
	public static String getIconSetDirectory() {
		return iconSetDirectory;
	}
	public static void setIconSetDirectory(String iconSetDirectory) {
		ShogiBoard.iconSetDirectory = iconSetDirectory;
	}
	public String getOpeningBoard() {
		return openingBoard.toString();
	}
	public void setOpeningBoard(String openingBoard) {
		this.openingBoard = openingBoard.toCharArray();
	}

	public JButton getaCapturedJButton() {
		return aCapturedJButton;
	}

	public void setaCapturedJButton(JButton aCapturedJButton) {
		this.aCapturedJButton = aCapturedJButton;
	}

	public JPanel getaCapturedJPanel() {
		return aCapturedJPanel;
	}

	public void setaCapturedJPanel(JPanel aCapturedJPanel) {
		this.aCapturedJPanel = aCapturedJPanel;
	}

	public ShogiPiece getaShogiPiece() {
		return aShogiPiece;
	}

	public void setaShogiPiece(ShogiPiece aShogiPiece) {
		this.aShogiPiece = aShogiPiece;
	}

	public ShogiSquare getaShogiSquare() {
		return aShogiSquare;
	}

	public void setaShogiSquare(ShogiSquare aShogiSquare) {
		this.aShogiSquare = aShogiSquare;
	}

	public ShogiController getControl() {
		return control;
	}

	public void setControl(ShogiController control) {
		this.control = control;
	}

	public Color getDefaultColor() {
		return defaultColor;
	}

	public void setDefaultColor(Color defaultColor) {
		this.defaultColor = defaultColor;
	}

	public Point getDestinationLocation() {
		return destinationLocation;
	}

	public void setDestinationLocation(Point destinationLocation) {
		this.destinationLocation = destinationLocation;
	}

	public JLayeredPane getDragPane() {
		return dragPane;
	}

	public void setDragPane(JLayeredPane dragPane) {
		this.dragPane = dragPane;
	}

	public ShogiPiece getDropPiece() {
		return dropPiece;
	}

	public void setDropPiece(ShogiPiece dropPiece) {
		this.dropPiece = dropPiece;
	}

	public JPanel getGameBoard() {
		return gameBoard;
	}

	public void setGameBoard(JPanel gameBoard) {
		this.gameBoard = gameBoard;
	}

	public boolean isIsLeftsTurn() {
		return isLeftsTurn;
	}

	public void setIsLeftsTurn(boolean isLeftsTurn) {
		this.isLeftsTurn = isLeftsTurn;
	}

	public JPanel getLeftCapturedPane() {
		return leftCapturedPane;
	}

	public void setLeftCapturedPane(JPanel leftCapturedPane) {
		this.leftCapturedPane = leftCapturedPane;
	}

	public Color getLeftColor() {
		return leftColor;
	}

	public void setLeftColor(Color leftColor) {
		this.leftColor = leftColor;
	}

	public Color getLegalColor() {
		return legalColor;
	}

	public void setLegalColor(Color legalColor) {
		this.legalColor = legalColor;
	}

	public void setOpeningBoard(char[] openingBoard) {
		this.openingBoard = openingBoard;
	}

	public Point getOriginalLocation() {
		return originalLocation;
	}

	public void setOriginalLocation(Point originalLocation) {
		this.originalLocation = originalLocation;
	}

	public ShogiSquare getOriginalSquare() {
		return originalSquare;
	}

	public void setOriginalSquare(ShogiSquare originalSquare) {
		this.originalSquare = originalSquare;
	}

	public JPanel getRightCapturedPane() {
		return rightCapturedPane;
	}

	public void setRightCapturedPane(JPanel rightCapturedPane) {
		this.rightCapturedPane = rightCapturedPane;
	}

	public Color getRightColor() {
		return rightColor;
	}

	public void setRightColor(Color rightColor) {
		this.rightColor = rightColor;
	}

	public char[] getSaveGame() {
		return saveGame;
	}

	public void setSaveGame(char[] saveGame) {
		this.saveGame = saveGame;
	}

	public int getxOffset() {
		return xOffset;
	}

	public void setxOffset(int xOffset) {
		this.xOffset = xOffset;
	}

	public int getyOffset() {
		return yOffset;
	}

	public void setyOffset(int yOffset) {
		this.yOffset = yOffset;
	}
	
}